<template>
	<view>
		<my-search 
			@searchHandler="gotoSearch"
		></my-search>
		<view class="cate_scroll__container">
			<scroll-view class="scroll_container__left" scroll-y="true" :style="{ height: screenAvailHeight + 'px'}">
				<block v-for="(item, index) in cateList" :key="index">
					<view :class="['scroll_left__item', index === activeIndex ? 'active' : '' ]" @click="activeChanged(index)">{{item.cat_name}}</view>
				</block>
			</scroll-view>
			<scroll-view class="scroll_container__right" scroll-y="true" :style="{ height: screenAvailHeight + 'px'}" :scroll-top="scrollTop">
				<view  v-for="(item2, index2) in cateLevel2List" :key="index2">
					<view class="scroll_right__title">/{{item2.cat_name}}/</view>
				
					<view class="scroll_right__list">
						<view class="scroll_list__item" v-for="(item3, index3) in item2.children" :key="index3" @click="getGoodsDetail(item3)">
							<image class="list_item__img" :src="item3.cat_icon"></image>
							<text class="list_item__text">{{item3.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
import { cateApi } from '../../static/api'
import { showMsg } from '../../static/common'
import badgeMix from '@/mixins/tabbar-badge'
	export default {
		mixins: [badgeMix],
		data() {
			return {
				screenAvailHeight: 0,
				cateList: [],	//左侧分类数据列表
				activeIndex: 0,
				cateLevel2List: [],
				scrollTop: 0,
				
			};
		},
		
		onLoad() {
			this.getScreenAvailHeight();
			this.getCateList();
		},
		
		methods: {
			getScreenAvailHeight() {
				const sysInfo = uni.getSystemInfoSync();
				this.screenAvailHeight = sysInfo.windowHeight;
			},
			
			async getCateList() {
				await cateApi.getCateList()
				.then(({ data }) => {
					if(data.meta.status !== 200) return showMsg.showErrMsg();
					this.cateList = data.message;
					this.cateLevel2List = data.message[0].children;
				})
				.catch((err) => {
					console.log(err);
				});
			},
			
			activeChanged(index) {
				this.activeIndex = index;
				this.cateLevel2List = this.cateList[index].children;
				this.scrollTop = this.scrollTop ? 0 : 1;
			},
			
			getGoodsDetail(goods) {
				uni.navigateTo({
					url: '/subpkg/goods_details/goods_details?goods_id=' + goods.cat_id,
				})
			},
			
			gotoSearch() {
				uni.navigateTo({
					url: '/subpkg/cate-search/cate-search',
				})
			},
		},
	}
</script>

<style lang="scss">
.cate_scroll__container {
	display: flex;
	.scroll_container__left {
		width: 120px;
		.scroll_left__item {
			background-color: #f7f7f7;
			line-height: 50px;
			text-align: center;
			font-size: 12px;
			&.active {
				position: relative;
				background-color: #fff;
				&::before {
					content: '';
					display: block;
					width: 3px;
					height: 30px;
					background-color: #C00000;
					position: absolute;
					top: 50%;
					transform: translateY(-50%);
				}
			}
		}
	}
	.scroll_container__right {
		background-color: #fff;
		.scroll_right__title {
			font-weight: 700;
			text-align: center;
			margin: 15rpx 0;
		}
		.scroll_right__list {
			display: flex;
			flex-wrap: wrap;
			.scroll_list__item {
				width: 33.33%;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				margin-bottom: 10rpx;
				.list_item__img {
					width: 60px;
					height: 60px;
				}
				.list_item__text {
					font-size: 12rpx;
				}
			}
		}
	}
}
</style>
